//
//  ScoreView.m
//  Checkers2015
//
//  Created by Dmitry Tatarinov on 2/19/15.
//  Copyright (c) 2015 Huly-Gun Soft. All rights reserved.
//

#import "ScoreView.h"

#import "vars.h"
#import "game_const.h"

@implementation ScoreView

- (id)initWithFrame:(CGRect) frame andDelegate: (id) vcDelegate
{
    if (self = [super initWithFrame:frame])
    {
        [self setupInterfaceItems];
        delegate = vcDelegate;
    }
    
    return self;
}

- (void)setupInterfaceItems
{
    CGFloat ofset = 32.0;
    CGFloat labelHeight = 48.0;

    UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, labelHeight)];
    scoreLabel.center = CGPointMake(self.frame.size.width/2, ofset * 3);
    scoreLabel.text = @"SCORE:";
    scoreLabel.textColor = [UIColor whiteColor];
    scoreLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:scoreLabel];
    
    CGFloat btnWidth = 128.0;
    CGFloat btnHeight = 48.0;
    
    UIButton *_nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nextBtn setTitle:@"Next Round" forState:UIControlStateNormal];
    [_nextBtn setFrame:CGRectMake(0, 0, btnWidth, btnHeight)];
    _nextBtn.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 - ofset);
    _nextBtn.tag = 100;
    [_nextBtn addTarget:self action:@selector(scoreViewBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_nextBtn];
    
    UIButton *_restartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_restartBtn setTitle:@"Restart" forState:UIControlStateNormal];
    [_restartBtn setFrame:CGRectMake(0, 0, btnWidth, btnHeight)];
    _restartBtn.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 + ofset);
    _restartBtn.tag = 200;
    [_restartBtn addTarget:self action:@selector(scoreViewBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_restartBtn];
    
    UIButton *_backToMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backToMenuBtn setTitle:@"Menu" forState:UIControlStateNormal];
    [_backToMenuBtn setFrame:CGRectMake(0, 0, btnWidth, btnHeight)];
    _backToMenuBtn.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 + ofset * 3);
    _backToMenuBtn.tag = 300;
    [_backToMenuBtn addTarget:self action:@selector(scoreViewBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backToMenuBtn];
}

- (void)scoreViewBtnPressed: (id) sender
{
    NSLog(@"pressed: %li", (long)[sender tag]);

    if ([sender tag] == 100){
    
        [self setupGamePosition];
   
        if (whiteStartLine < 0 | blackStartLine < 0)
        {
            if (gameProhod < PROHOD_COUNT)
            {
                gameProhod++;
                blackStartLine = 0;
                whiteStartLine = 0;
            }
            else
            {
                NSLog(@"Game ENDED");
                
                [delegate unloadGame];
                //[delegate loadLevelSelect];
                [delegate loadGameScore];
                return; //must!!!
            }
        }
     
        [delegate startGame];
    }
    
    if ([sender tag] == 200)
    {
        //cur score = 0

        [delegate startGame];
    }
    
    if ([sender tag] == 300)
    {
        [delegate unloadGame];
        [delegate loadLevelSelect];
    }
    
    [self removeFromSuperview];
}

- (void)setupGamePosition
{

    lastGameBlackCount = 0;  /// DEBUG!!!

    //белые победили
    if (lastGameBlackCount == 0)
    {
        whiteVictoryCount++;
        whiteStartLine++;
    }
    //черные победили
    if (lastGameWhiteCount == 0)
    {
        blackVictoryCount++;
        blackStartLine++;
    }
    
    // отодвигаем проигравшего если надо
    if ((lastGameBlackCount==0 | lastGameWhiteCount==0) & (blackStartLine + whiteStartLine > MOVE_POROG[gameProhod]))
    {
        if (lastGameBlackCount > lastGameWhiteCount) whiteStartLine--;
        else blackStartLine--; 
    }

}

@end
